package com.zukxu.items.project.pictures;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 通过输入内容生成纯色背景的图片
 * </p>
 *
 * @author zukxu
 * @date 2020/12/7 0007 10:59
 */
public class StrToImg {
	private static final String FILE_PATH = "E:/temp/";

	//初始化存储路径
	static {
		File file = new File(FILE_PATH);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}
	}

	public static void main(String[] args) throws IOException {
		String[] name = {"栩", "x", "栩栩", "xu", "栩U", "X栩", "%&","栩栩如生","xu栩如生"};
		for (String s : name) {
			generateImg(s.trim());
		}
		System.out.println("生成结束");
	}

	/**
	 * 绘制字体头像
	 * 如果是英文名，只显示首字母大写
	 * 如果是中文名，只显示最后两个字
	 *
	 * @param name 用户名
	 * @throws IOException
	 */
	public static void generateImg(String name) throws IOException {
		//定义头像大小
		int width = 100;
		int height = 100;
		//如果用户输入的姓名小于等于2个字符，不用截取，直接输出
		if (name.length() > 2) {
			//根据首字母判断如何截取，中文截取末两位，英文截取首两位
			name = subStr(name);
		}
		//	生成图片
		initImg(width, height, name);
	}

	/**
	 * 初始化图片
	 *
	 * @param width   图片宽度
	 * @param height  图片高度
	 * @param outName 输出内容
	 * @throws IOException
	 */
	private static void initImg(int width, int height, String outName) throws IOException {
		//文件名生成
		long timeMillis = System.currentTimeMillis();
		String filename = FILE_PATH + outName + timeMillis + ".jpg";
		File file = new File(filename);
		//初始化图像
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		//2D
		Graphics2D g2 = (Graphics2D) bi.getGraphics();
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setBackground(getRandomColor());
		g2.clearRect(0, 0, width, height);
		g2.setPaint(Color.WHITE);
		//设置默认字体样式
		g2.setFont(new Font("微软雅黑", Font.PLAIN, 32));
		judgeMode(g2, outName);
		//圆角处理
		BufferedImage rounded = makeRoundedCorner(bi, 99);
		//输出
		ImageIO.write(rounded, "png", file);
	}

	/**
	 * 判断绘图模式,根据内容进行判断
	 *
	 * @param g2
	 * @param outName
	 */
	public static void judgeMode(Graphics2D g2, String outName) {
		if (outName.length() < 2) {
			if (isChinese(outName)) {
				g2.setFont(new Font("华文行楷", Font.PLAIN, 56));
				g2.drawString(outName, 24, 64);
			} else {
				//英文
				g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
				g2.drawString(outName.toUpperCase(), 28, 72);
			}
			return;
		}
		String firstLetter = outName.substring(0, 1);
		String secondLetter = outName.substring(1, 2);
		//两个中文 如 自由客
		if (isChinese(firstLetter) && isChinese(secondLetter)) {
			g2.drawString(outName, 16, 56);
		} else if (!isChinese(firstLetter) && !isChinese(secondLetter)) {
			//两个英文 如 ZUK
			g2.setFont(new Font("Snap ITC", Font.PLAIN, 36));
			g2.drawString(outName.toUpperCase(), 24, 64);
		} else {
			//首中次英 如 自U客
			if (isChinese(firstLetter)) {
				g2.setFont(new Font("微软雅黑", Font.BOLD, 32));
				g2.drawString(firstLetter, 24, 56);
				g2.setFont(new Font("微软雅黑", Font.PLAIN, 30));
				g2.drawString(secondLetter, 56, 64);
			} else {
				//首英次中 如 Z由客
				g2.setFont(new Font("微软雅黑", Font.PLAIN, 40));
				g2.drawString(firstLetter, 24, 56);
				g2.setFont(new Font("微软雅黑", Font.PLAIN, 32));
				g2.drawString(secondLetter, 56, 64);
			}
		}
	}

	/**
	 * 判断截取位置
	 *
	 * @param name
	 * @return
	 */
	private static String subStr(String name) {
		//根据首字母判断如何进行截取
		String first = name.substring(0, 1);
		if (isChinese(first)) {
			//截取末两位
			name = name.substring(name.length() - 2);
		} else {
			//截取首两位，并设置为大写状态
			name = name.substring(0, 2).toUpperCase();
		}
		return name;
	}


	/**
	 * 判断字符串是否为中文
	 *
	 * @param str
	 * @return
	 */
	public static boolean isChinese(String str) {
		String regEx = "[\\u4e00-\\u9fa5]+";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}

	/**
	 * 获得随机颜色
	 *
	 * @return
	 */
	private static Color getRandomColor() {
		String[] beautifulColors =
				new String[]{"232,221,203", "205,179,128", "3,101,100", "3,54,73", "3,22,52",
						"237,222,139", "251,178,23", "96,143,159", "1,77,103", "254,67,101", "252,157,154",
						"249,205,173", "200,200,169", "131,175,155", "229,187,129", "161,23,21", "34,8,7",
						"118,77,57", "17,63,61", "60,79,57", "95,92,51", "179,214,110", "248,147,29",
						"227,160,93", "178,190,126", "114,111,238", "56,13,49", "89,61,67", "250,218,141",
						"3,38,58", "179,168,150", "222,125,44", "20,68,106", "130,57,53", "137,190,178",
						"201,186,131", "222,211,140", "222,156,83", "23,44,60", "39,72,98", "153,80,84",
						"217,104,49", "230,179,61", "174,221,129", "107,194,53", "6,128,67", "38,157,128",
						"178,200,187", "69,137,148", "117,121,71", "114,83,52", "87,105,60", "82,75,46",
						"171,92,37", "100,107,48", "98,65,24", "54,37,17", "137,157,192", "250,227,113",
						"29,131,8", "220,87,18", "29,191,151", "35,235,185", "213,26,33", "160,191,124",
						"101,147,74", "64,116,52", "255,150,128", "255,94,72", "38,188,213", "167,220,224",
						"1,165,175", "179,214,110", "248,147,29", "230,155,3", "209,73,78", "62,188,202",
						"224,160,158", "161,47,47", "0,90,171", "107,194,53", "174,221,129", "6,128,67",
						"38,157,128", "201,138,131", "220,162,151", "137,157,192", "175,215,237", "92,167,186",
						"255,66,93", "147,224,255", "247,68,97", "185,227,217"};
		int len = beautifulColors.length;
		Random random = new Random();
		String[] color = beautifulColors[random.nextInt(len)].split(",");
		return new Color(Integer.parseInt(color[0]), Integer.parseInt(color[1]),
				Integer.parseInt(color[2]));
	}


	/**
	 * 图片做圆角处理
	 *
	 * @param image
	 * @param cornerRadius
	 * @return
	 */
	public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
		int w = image.getWidth();
		int h = image.getHeight();
		BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = output.createGraphics();
		g2.setComposite(AlphaComposite.Src);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(Color.WHITE);
		g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
		g2.setComposite(AlphaComposite.SrcAtop);
		g2.drawImage(image, 0, 0, null);
		g2.dispose();
		return output;
	}
}